# import hashlib
# import xml.etree.ElementTree as ET
#
# from api.utils import random_32_str
#
#
# class WeixinUnifiedOrderRequest:
#     def __init__(self, out_trade_no, total_fee, spbill_create_ip,
#                  time_start, time_expire):
#         self.app_id = 'wx3cc2a7f7e331c97f'
#         self.mch_id = '1485369362'
#         self.device_info = 'WEB'
#         self.nonce_str = random_32_str()
#         self.body = u'华焱农贸-消费'
#         self.detail = None
#         self.attach = None
#         self.out_trade_no = out_trade_no
#         self.fee_type = 'CNY'
#         self.total_fee = total_fee
#         self.spbill_create_ip = spbill_create_ip
#         self.time_start = time_start
#         self.time_expire = time_expire
#         self.goods_tag = None
#         self.notify_url = "https://"
#         self.trade_type = 'APP'
#         self.limit_pay = 'no_credit'
#         self.sign_type = 'MD5'
#
#     @property
#     def sign(self):
#         data = {
#             'app_id': self.app_id,
#             'mch_id': self.mch_id,
#             'device_info': self.device_info,
#             'nonce_str': self.nonce_str,
#             'body': self.body,
#             'out_trade_node': self.out_trade_no,
#             'fee_type': self.fee_type,
#             'total_fee': self.total_fee,
#             'spbill_create_ip': self.spbill_create_ip,
#             'time_start': self.time_start,
#             'time_expire': self.time_expire,
#             'notify_url': self.notify_url,
#             'trade_type': self.trade_type,
#             'limit_pay': self.limit_pay
#         }
#
#         if self.detail:
#             data['detail'] = self.detail
#
#         if self.attach:
#             data['attach'] = self.attach
#
#         if self.goods_tag:
#             data['goods_tag'] = self.goods_tag
#
#         unsigned_str = ''
#         sorted_key = list(data.keys())
#         sorted_key.sort()
#         for i, key in enumerate(sorted_key):
#             if i + 1 == len(sorted_key):
#                 unsigned_str = '%s=%s' % (key, data[key])
#             else:
#                 unsigned_str = '%s=%s&' % (key, data[key])
#
#         md5 = hashlib.md5()
#         md5.update(unsigned_str.encode('UTF-8'))
#         return md5.hexdigest()
#
#     def to_xml(self):
#         xml = ET.Element('xml')
#         ET.SubElement(xml, 'app_id').text = self.app_id
#         ET.SubElement(xml, 'mch_id').text = self.mch_id
#         ET.SubElement(xml, 'device_info').text = self.device_info
#         ET.SubElement(xml, 'nonce_str').text = self.nonce_str
#         ET.SubElement(xml, 'body').text = self.body
#         if self.detail:
#             ET.SubElement(xml, 'detail').text = self.detail
#         if self.attach:
#             ET.SubElement(xml, 'attach').text = self.attach
#         ET.SubElement(xml, 'out_trade_no').text = self.out_trade_no
#         ET.SubElement(xml, 'fee_type').text = self.fee_type
#         ET.SubElement(xml, 'total_fee').text = self.total_fee
#         ET.SubElement(xml, 'spbill_create_ip').text = self.spbill_create_ip
#         ET.SubElement(xml, 'time_start').text = self.time_start
#         ET.SubElement(xml, 'time_expire').text = self.time_expire
#         if self.goods_tag:
#             ET.SubElement(xml, 'goods_tag').text = self.goods_tag
#         ET.SubElement(xml, 'notify_url').text = self.notify_url
#         ET.SubElement(xml, 'trade_type').text = self.trade_type
#         ET.SubElement(xml, 'limit_pay').text = self.limit_pay
#         ET.SubElement(xml, 'sign').text = self.sign
#         ET.SubElement(xml, 'sign_type').text = self.sign_type
#         return ET.tostring(xml)
from rest_framework.exceptions import ValidationError

from .pywxpay import WXPay


def weixin_pay(out_trade_no, total_fee, spbill_create_ip):
    wxpay = WXPay(app_id='wx3cc2a7f7e331c97f',
                  mch_id='1485369362',
                  key='b48634c73fcdf62513d23a26e6c66b4f',
                  cert_pem_path='/path/to/apiclient_cert.pem',
                  key_pem_path='/path/to/apiclient_key.pem',
                  timeout=6000)  # 毫秒

    wxpay_resp_dict = wxpay.unifiedorder(dict(device_info='WEB',
                                              body='华焱农贸-消费',
                                              detail='',
                                              out_trade_no=out_trade_no,
                                              total_fee=total_fee,
                                              fee_type='CNY',
                                              notify_url='http://www.example.com/wxpay/notify',
                                              spbill_create_ip=spbill_create_ip,
                                              trade_type='NATIVE')
                                         )

    if 'SUCCESS' == wxpay_resp_dict['return_code']:
        pass
    else:
        raise ValidationError(detail=wxpay_resp_dict['return_msg'])  # 支付失败
